#include "fontmanager.h"


FontManager::FontManager(QObject *parent)
    : QObject{parent}
{

}

QString FontManager::loadFont(const QString &fontPath)
{
    int fontId = m_fontDatabase.addApplicationFont(fontPath);
    if (fontId == -1) {
        qWarning() << "Failed to load font from:" << fontPath;
        return QString();
    }

    QStringList families = m_fontDatabase.applicationFontFamilies(fontId);
    if (families.isEmpty()) {
        qWarning() << "Loaded font has no family name:" << fontPath;
        return QString();
    }

    m_loadedFamilies.append(families);

    for (int var = 0; var < families.count(); ++var) {
       qDebug() << "get font family:" << families.at(var);
    }
    return families.first(); // 返回第一个字体家族名称
}

bool FontManager::setGlobalFont(const QString &family,int PixelSize)
{
    if (!m_loadedFamilies.contains(family)) {
        qWarning() << "Font family not loaded:" << family;
        return false;
    }

    QFont font(family);
    if (PixelSize > 0) {
        font.setPixelSize(PixelSize);
    }

    QGuiApplication::setFont(font);
    qDebug() << "Global font set to:" << family;
    return true;
}

QStringList FontManager::loadedFontFamilies() const
{
    return m_fontDatabase.families();
}
